<?php


error_reporting(E_ALL);


defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

# 自动加载
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

# 引入类
use app\components\LogComponent;
use app\events\LogEvent;
use yii\base\Event;
use app\behaviors\LogBehavior;
use yii\behaviors\SluggableBehavior;
use yii\di\ServiceLocator;
use yii\di\Container;

# 设置路径
Yii::setAlias('@app', realpath('./'));

# 实例化组件
## 1
$logConfig = [
    'class' => LogComponent::class,
    'prefix' => function () {
        return '';
    },
    // 'as StdLog' => 'app\behaviors\LogBehavior',
    'on ' . LogComponent::EVENT_BEFORE_LOG => function ($event) {
        echo 'event', PHP_EOL;
    },
    // 'as LogBehavior' => 'app\behaviors\LogBehavior',
];

$log = Yii::createObject($logConfig);

## 2
// $locator = new ServiceLocator;
// $locator->set('log', $logConfig);
// $log = $locator->log;

// $log = $locator->get('log');
// 对象级别事件
// $log->on(LogComponent::EVENT_BEFORE_LOG, function ($event) {
//     echo 'event:2', PHP_EOL;
// });

// 类级事件
LogEvent::on(LogComponent::class, '*', function ($event) {
    echo 'event:', mt_rand(1, 200),PHP_EOL;
});

LogEvent::off(LogComponent::class, '*');
LogEvent::off(LogComponent::class, LogComponent::EVENT_BEFORE_LOG);

// $log->encoding = 'text';

// 触发任何类的任何事件
// Event::on('*', '*', function ($event) {
//     echo('trigger event: ' . $event->name . PHP_EOL);
// });


// $log->attachBehaviors([['class'=>LogBehavior::class,'encoding'=>'json']]);
// $log->cate('api')->level('error')->log('log');

$log->log('hahah');
// LogEvent::trigger(LogComponent::class, LogComponent::EVENT_BEFORE_LOG);


// class Base{
// 	public $name = '';
// 	public function __construct(Depen $d)
//     {
// 	}
// }

// class Depen
// {
// }

// $container = new Container;


// $base = $container->get('Base');

// var_dump($base);
